package com.zzyy.study.controller;

import com.zzyy.study.exp.BusinessException;
import com.zzyy.study.resp.ResultData;
import com.zzyy.study.resp.ReturnCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @auther zzyy
 * @create 2024-05-12 17:21
 */
@RestController
public class HelloOpenAIController
{
    @Resource private OpenAiChatClient chatClient;

    //http://localhost:9900/ai/hello?ask=%E4%B8%8A%E6%B5%B7%E6%9C%89%E5%87%A0%E4%B8%AA%E5%8C%BA
    @GetMapping("/ai/hello")
    public String helloV1(@RequestParam("ask") String ask)
    {
        long startTime = System.currentTimeMillis();
        String retValue =  chatClient.call(ask);
        System.out.println(retValue);
        long endTime = System.currentTimeMillis();
        System.out.println("----costTime: "+(endTime - startTime) +" 毫秒");
        return retValue;
    }

    @GetMapping("/ai/hellov2")
    public ResultData<String> helloV2(@RequestParam("ask") String ask)
    {
        if (Objects.isNull(ask)) {
            throw new BusinessException(ReturnCodeEnum.RC1000.getCode(),ReturnCodeEnum.RC1000.getMessage());
        }

        long startTime = System.currentTimeMillis();
        String retValue =  chatClient.call(ask);
        long endTime = System.currentTimeMillis();
        System.out.println("----costTime: "+(endTime - startTime) +" 毫秒");

        return ResultData.success(retValue);
    }

}
